package com.mofang.powerdekorhelper.base.recycleview;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

import com.mofang.powerdekorhelper.utils.L;
import com.mofang.powerdekorhelper.witget.MyRecycleItemAnimator;
import com.mofang.powerdekorhelper.witget.RefreshItemDecoration;

/**
 * Created by luofei on 2016/9/20 0020.
 */
public class BaseRecycleViewList extends BaseRecycleView {

    MyRecycleOnScrollListener myOnScrollListener;
    LinearLayoutManager linearLayoutManager;

    public BaseRecycleViewList(Context context, AttributeSet attrs) {
        super(context, attrs);
        linearLayoutManager = new LinearLayoutManager(context);
        setLayoutManager(linearLayoutManager);
        setItemAnimator(new MyRecycleItemAnimator());
        addItemDecoration(new RefreshItemDecoration(context, RefreshItemDecoration.VERTICAL_LIST));
    }

    public BaseRecycleViewList(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public BaseRecycleViewList(Context context) {
        super(context);
    }

    public void setListOnScrollListener(MyRecycleOnScrollListener onScrollListener) {
        addOnScrollListener(listener);
        myOnScrollListener = onScrollListener;
    }

    OnScrollListener listener = new OnScrollListener() {

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            L.e("Loading", "onScrollStateChanged---------->" + newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            L.e("Loading", "onScrolled---------->");
            if (isSlideToBottom2(recyclerView)) {
                L.e("Loading", "onScrolled---------->isBottom");
                myOnScrollListener.loadMore();
            }
        }
    };

    //判断是否滑到底部
    protected boolean isSlideToBottom2(RecyclerView recyclerView) {
        if (recyclerView.canScrollVertically(1)) {
            return false;
        } else {
            return true;
        }
    }

    // 滑动监听
    public interface MyRecycleOnScrollListener {
        void loadMore();
    }

    public void setOrientation(int orientation) {
        linearLayoutManager.setOrientation(orientation);
        setLayoutManager(linearLayoutManager);
    }

}
